﻿
@{
    ViewData["Title"] = "站点";
}

@*<p>
        <a asp-action="Create">增加功能</a>
    </p>*@

<div class="breadcrumbs ace-save-state" id="breadcrumbs">
    <ul class="breadcrumb">
        <li>
            <i class="ace-icon fa fa-home home-icon"></i>
            <a href="~/Home/Index">首页</a>
        </li>
        <li>
            <a href="#">基础设置</a>
        </li>
        <li class="active">站点</li>
    </ul>

</div>

<div class="row">
    <div class="col-xs-12">
        <h3 class="row header smaller lighter blue">
            <dic class="col-sm-7">

            </dic><!-- /.col -->
            <dic class="col-sm-5">
                <label class="pull-right inline">
                    <button id="btnAdd" title="" class="btn btn-sm btn-primary" type="button" data-original-title="新增功能" data-toggle="tooltip" data-widget="">
                        <i class="fa fa-plus-circle"></i>&nbsp;&nbsp;新增
                    </button>
                    <button id="btnEdit" title="" class="btn btn-sm btn-primary" type="button" data-original-title="编辑功能" data-toggle="tooltip" data-widget="">
                        <i class="fa fa-edit"></i>&nbsp;&nbsp;编辑
                    </button>

                    <button id="btnDelete" title="" class="btn btn-sm btn-danger" type="button" data-original-title="删除功能" data-toggle="tooltip" data-widget="">
                        <i class="fa fa-times"></i>&nbsp;&nbsp;删除
                    </button>
                </label>
            </dic><!-- /.col -->
        </h3>

        <div class="col-md-3" style="border:solid 1px  gray;">
            <div class="box box-primary">

                <div class="box-body">
                    <div id="treeDiv" class="portlet-body" style="min-height:300px;">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-9">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <table class="table table-striped table-bordered table-hover dataTable no-footer" role="grid" aria-describedby="sample_2_info">
                        <thead>
                            <tr role="row">
                                <th class="table-checkbox" style="width:40px;text-align:center;"><input id="checkAll" class="group-checkable" type="checkbox"></th>
                                <th tabindex="0" aria-label="">
                                    序号
                                </th>
                                <th tabindex="0" aria-label="">
                                    站点名称
                                </th>
                                <th tabindex="0" aria-label="">
                                    站点网址
                                </th>
                                <th tabindex="0" aria-label="">
                                    站点类型
                                </th>
                                <th tabindex="0" aria-label="">
                                    排序值
                                </th>
                                <th tabindex="0" style="width: 230px;" aria-label="">
                                    订单排序类型
                                </th>
                                <th tabindex="0" aria-label="">
                                    站点描述
                                </th>

                                <th tabindex="0" aria-label="">
                                    最大预约时间
                                </th>

                            </tr>
                        </thead>
                        <tbody id="tableBody"></tbody>
                    </table>
                    <div id="grid_paging_part"></div>



                </div>

            </div>
        </div>
    </div>
</div>
@Html.Partial("Edit")
<script>
    var pageSize = 20;
    var selectedParentId = 0;
    var OddOrderTypes = [{ id: 1, text: "发单时间" }, { id: 2, text: "最大比赛编号" }];
    var AllSites = [];
    var ss;
    var ot;
    $(function () {
        $(document).attr("title", "站点");
        ot=$("#OddOrderType").select2({
            minimumResultsForSearch: -1,
            width:150
        });
        ss = $("#ParentId").select2({
            width:150

        });
        $("#btnAdd").click(function () { Add(); });
        $("#btnDelete").click(function () { DeleteMulti(); });
        $("#btnSave").click(function () { Save(); });
        $("#btnEdit").click(function () { Edit(this); });
        
        $("#checkAll").click(function () { CheckAll(this) });
        InitTree();
        LoadTables();
    });



    //加载功能树
    function InitTree() {
        $.jstree.destroy();
        AjaxData("/Site/GetSiteTreeData?_t=" + new Date().getTime(), "", "Get", null, function (d) {
            if (d.success) {
                var s = '';
                $.each(d.data, function (i, n) { 

                    s += '<option value="' + n.id + '">' + n.text+'</option>';
                });
                $("#ParentId").html('');
                $("#ParentId").html(s);
                
                $('#treeDiv').jstree({       //创建JsTtree
                    'core': {
                        'data': d.data,        //绑定JsTree数据
                        "multiple": false    //是否多选
                    },
                    "plugins": ["state", "types", "wholerow"]  //配置信息
                });
                $("#treeDiv").on("ready.jstree", function (e, data) {   //树创建完成事件
                    data.instance.open_all();    //展开所有节点
                    if (selectedParentId == 0) {
                        data.instance.select_node(1);
                    }
                });
                $("#treeDiv").on('changed.jstree', function (e, data) {   //选中节点改变事件

                    var node = data.instance.get_node(data.selected[0]);  //获取选中的节点
                    if (node) {
                       
                        selectedParentId = node.id;
                        LoadTables(1, pageSize);
                    };
                });
            }
        });
    }



    //加载列表数据
    function LoadTables(startPage,pageSize) {
        $("#tableBody").html("");
        $("#checkAll").prop("checked", false);
        AjaxData("/Site/GetSitesByParentId?id=" + selectedParentId +"&startPage=" + startPage + " &pageSize=" + pageSize + "&_t=" + new Date().getTime(), "", "Get", "", function (d) {
            if (d.success) { 
                var data = d.data;
                $.each(data.data, function (i, item) {
                    var tr = "<tr>";
                    tr += "<td align='center'><input type='checkbox' class='checkboxs' value='" + item.id + "'/></td>";
                    tr += "<td>" + (i + 1) + "</td>";
                    tr += "<td>" + item.name + "</td>";
                    tr += "<td>" + item.url + "</td>";
                    tr += "<td>" + item.type + "</td>";
                    tr += "<td>" + item.sortValue + "</td>";
                    tr += "<td>" + OddOrderTypes.FirstOrDefault(function (x) { return x.id == item.oddOrderType; }).text + "</td>";
                    tr += "<td>" + item.remark + "</td>";
                    tr += "<td>" + item.maxBespeakTime + "</td>";
                    //tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='Edit(\"" + item.id + "\")'><i class='fa fa-edit'></i> 编辑 </button> </td>";
                    tr += "</tr>";
                    $("#tableBody").append(tr);
                })
                $("#grid_paging_part").lagerPaper({
                    currentIndex: data.pageIndex,
                    pageSize: pageSize,
                    totalCount: data.allCount,
                    pageCount: data.pageCount,
                    onPageChanged: function (e, pageIndex) {
                        LoadTables(pageIndex, pageSize);
                    }
                });
            }
        });
       
    }
    //全选
    function CheckAll(obj) {
        $(".checkboxs").each(function () {
            if (obj.checked == true) {
                $(this).prop("checked", true);

            }
            if (obj.checked == false) {
                $(this).prop("checked", false);
            }
        });
    };
    //新增
    function Add() {

        if (selectedParentId < 0) { 

            layer.alert("请在左边选择父站点");
            return;
        }
        var dialog = $("#editModal").removeClass('hide').dialog({
            modal: true,
            title: '新增站点',
            title_html: false,
            width: 650,
            height: 550

        });

        $("#Id").val(-1);
        $("#Name").val("");
        $("#Url").val("");
        ss.val("").trigger("change");
        $("#SortValue").val("");
        $("#Remark").val("");
        $("#OddOrderType").val("");
        $("#MaxBespeakTime").val("");
        $("#Title").text("新增站点");
        
    };
    //编辑
    function Edit(selector) {

        var dialog = $("#editModal").removeClass('hide').dialog({
            modal: true,
            title: '编辑站点',
            title_html: false,
            width: 650,
            height: 550

        });

        var ids = [];
        $(".checkboxs").each(function () {
            if ($(this).prop("checked") == true) {
                ids.push($(this).val());
            }
        });
        if (ids.length == 0) {
            layer.tips("请选择要编辑的记录", selector);
            return;
        };
        if (ids.length > 1) {
            layer.tips("不能同时编辑多条记录", selector);
            return;
        }

        $.ajax({
            type: "Get",
            url: "/Site/Get?id=" + ids[0] + "&_t=" + new Date().getTime(),
            success: function (d) {
                var data = d.data;
                ss.val(data.parentId).trigger("change");
                $("#Id").val(data.id);
                $("#Name").val(data.name);
                ot.val(data.oddOrderType).trigger("change");
                $("#Type").val(data.type);
                $("#Url").val(data.url);
                $("#SortValue").val(data.sortValue);
                $("#Remark").val(data.remark);
                $("#MaxBespeakTime").val(data.maxBespeakTime);
            }
        })
    };
    //保存
    function Save() {
        var postData = {
            "dto":
            {
                "Id": $("#Id").val(),
                "ParentId":ss.val(),
                "Name": $("#Name").val(),
                "Url": $("#Url").val(),
                "Remark": $("#Remark").val(),
                "OddOrderType":ot.val(),
                "Type": $("#Type").val(),
                "SortValue": $("#SortValue").val(),
                "MaxBespeakTime": $("#MaxBespeakTime").val(),
            }
        };
        $.ajax({
            type: "Post",
            url: "/Site/Edit",
            data: postData,
            success: function (data) {
                if (data.success) {
                    InitTree();
                    //LoadTables(1, 50);
                    
                    $(".ui-dialog-titlebar-close").trigger("click");
                } else {
                    layer.tips(data.message, "#btnSave");
                };
            }
        });
    };
    //批量删除
    function DeleteMulti() {
        var ids = "";
        $(".checkboxs").each(function () {
            if ($(this).prop("checked") == true) {
                ids += $(this).val() + ","
            }
        });
        ids = ids.substring(0, ids.length - 1);
        if (ids.length == 0) {
            layer.alert("请选择要删除的记录。");
            return;
        };
        //询问框
        layer.confirm("您确认删除选定的记录吗？", {
            btn: ["确定", "取消"]
        }, function () {
            var sendData = { "ids": ids };
            $.ajax({
                type: "Post",
                url: "/Site/DeleteMuti",
                data: sendData,
                success: function (data) {
                    if (data.success) {
                        LoadTables(1, pageSize);
                        layer.closeAll();
                    }
                    else {
                        layer.alert("删除失败！" + data.message);
                    }
                }
            });
        });
    };

</script>




